home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / lame_src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-01  |  4.2 KB  |  171 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #ifdef _WIN32
  6. /* needed to set stdout to binary */
  7. #include <io.h>
  8. #endif
  9. #include "lame.h"
  10. #include "util.h"
  11.  
  12. #ifdef HAVEGTK
  13. #include "gtkanal.h"
  14. #include <gtk/gtk.h>
  15. #endif
  16.  
  17. #ifdef __riscos__
  18. #include "asmstuff.h"
  19. #endif
  20.  
  21.  
  22.  
  23.  
  24. /************************************************************************
  25. *
  26. * main
  27. *
  28. * PURPOSE:  MPEG-1,2 Layer III encoder with GPSYCHO
  29. * psychoacoustic model.
  30. *
  31. ************************************************************************/
  32.  
  33.  
  34. int main(int argc, char **argv)
  35. {
  36.  
  37.   char mp3buffer[LAME_MAXMP3BUFFER];
  38.   short int Buffer[2][1152];
  39.   int iread,imp3;
  40.   lame_global_flags gf;
  41.   FILE *outf;
  42. #ifdef __riscos__
  43.   int i;
  44. #endif
  45.  
  46.  
  47.   if (lame_init(&gf)<0) LAME_ERROR_EXIT();       /* initialize libmp3lame */
  48.   if(argc==1) lame_usage(&gf,argv[0]);  /* no command-line args, print usage, exit  */
  49.  
  50.   /* parse the command line arguments, setting various flags in the
  51.    * struct 'gf'.  If you want to parse your own arguments,
  52.    * or call libmp3lame from a program which uses a GUI to set arguments,
  53.    * skip this call and set the values of interest in the gf struct.
  54.    * (see lame.h for documentation about these parameters)
  55.    */
  56. #ifdef ONLYVORBIS
  57.   gf.ogg=1;
  58. #endif
  59.   lame_parse_args(&gf,argc, argv);
  60.  
  61.  
  62.  
  63.   /* open the wav/aiff/raw pcm or mp3 input file.  This call will
  64.    * open the file with name gf.inFile, try to parse the headers and
  65.    * set gf.samplerate, gf.num_channels, gf.num_samples.
  66.    * if you want to do your own file input, skip this call and set
  67.    * these values yourself.
  68.    */
  69.   lame_init_infile(&gf);
  70.  
  71.   /* Now that all the options are set, lame needs to analyze them and
  72.    * set some more options
  73.    */
  74.   if (lame_init_params(&gf)<0)  LAME_ERROR_EXIT();
  75.  
  76.   if (!gf.decode_only)
  77.     lame_print_config(&gf);   /* print usefull information about options being used */
  78.  
  79.  
  80.   if (!gf.gtkflag) {
  81.     /* open the output file */
  82.     if (!strcmp(gf.outPath, "-")) {
  83. #ifdef __EMX__
  84.       _fsetmode(stdout,"b");
  85. #elif (defined  __BORLANDC__)
  86.       setmode(_fileno(stdout), O_BINARY);
  87. #elif (defined  __CYGWIN__)
  88.       setmode(fileno(stdout), _O_BINARY);
  89. #elif (defined _WIN32)
  90.       _setmode(_fileno(stdout), _O_BINARY);
  91. #endif
  92.       outf = stdout;
  93.     } else {
  94.       if ((outf = fopen(gf.outPath, "wb")) == NULL) {
  95.     ERRORF("Could not create \"%s\".\n", gf.outPath);
  96.     LAME_ERROR_EXIT();
  97.       }
  98.     }
  99. #ifdef __riscos__
  100.     /* Assign correct file type */
  101.     for (i = 0; gf.outPath[i]; i++) {
  102.       if (gf.outPath[i] == '.')
  103.         gf.outPath[i] = '/';
  104.       else if (gf.outPath[i] == '/')
  105.         gf.outPath[i] = '.';
  106.     }
  107.     if (gf.decode_only)
  108.       SetFiletype(gf.outPath, 0xfb1); /* Wave */
  109.     else
  110.       SetFiletype(gf.outPath, 0x1ad); /* AMPEG */
  111. #endif
  112.   }
  113.  
  114.  
  115.   if (gf.gtkflag) {
  116.  
  117. #ifdef HAVEGTK
  118.     gtk_init (&argc, &argv);
  119.     gtkcontrol(&gf);
  120. #else
  121.     ERRORF("Error: lame not compiled with GTK support \n");
  122. #endif
  123.  
  124.   } else if (gf.decode_only) {
  125.  
  126.     /* decode an mp3 file to a .wav */
  127.     lame_decoder(&gf,outf,gf.encoder_delay);
  128.  
  129.   } else {
  130.  
  131.       /* encode until we hit eof */
  132.       do {
  133.     /* read in 'iread' samples */
  134.     iread=lame_readframe(&gf,Buffer);
  135.  
  136.  
  137.     /* encode */
  138.     imp3=lame_encode_buffer(&gf,Buffer[0],Buffer[1],iread,
  139.               mp3buffer,(int)sizeof(mp3buffer));
  140.  
  141.     /* was our output buffer big enough? */
  142.     if (imp3<0) {
  143.       if (imp3==-1) ERRORF("mp3 buffer is not big enough... \n");
  144.       else ERRORF("mp3 internal error:  error code=%i\n",imp3);
  145.       LAME_ERROR_EXIT();
  146.     }
  147.  
  148.     /* imp3 is not negative, but fwrite needs an unsigned here */
  149.     if (fwrite(mp3buffer,1,(unsigned int)imp3,outf) != imp3) {
  150.       ERRORF("Error writing mp3 output \n");
  151.       LAME_ERROR_EXIT();
  152.     }
  153.       } while (iread);
  154.       imp3=lame_encode_finish(&gf,mp3buffer,(int)sizeof(mp3buffer));   /* may return one more mp3 frame */
  155.       if (imp3<0) {
  156.     if (imp3==-1) ERRORF("mp3 buffer is not big enough... \n");
  157.     else ERRORF("mp3 internal error:  error code=%i\n",imp3);
  158.     LAME_ERROR_EXIT();
  159.       }
  160.       /* imp3 is not negative, but fwrite needs an unsigned here */
  161.       fwrite(mp3buffer,1,(unsigned int)imp3,outf);
  162.       fclose(outf);
  163.       lame_mp3_tags(&gf);                /* add id3 or VBR tags to mp3 file */
  164.     }
  165.   lame_close_infile(&gf);            /* close the input file */
  166.   return 0;
  167. }
  168.  
  169.  
  170.  
  171.